#!/usr/bin/env bash

requirements_centos_lib_installed()
{
  rpm -q --whatprovides "$1" >/dev/null || return $?
}

requirements_centos_lib_available()
{
  yum list --cacheonly --quiet "$1" >/dev/null 2>&1 || return $?
}

# required only for libyaml-devel check as fedora is broken ...
requirements_centos_lib_available_no_caching()
{
  yum list --quiet "$1" >/dev/null 2>&1 || return $?
}

requirements_centos_version_list()
{
  rpm -q --whatprovides "$1" | sed -e "s#^$1-##"
}

requirements_centos_install_custom()
{
  requirements_rvm_pkg_libs_install "$@" || return $?
}

requirements_centos_libs_install()
{
  __rvm_try_sudo yum install -y "$@" || return $?
}

requirements_centos_before_update_check_repos_missing_error()
{
  rvm_error "There is no '$*' package available for installation and RVM does not know how to make that happen,
please tell us how to install '$*' on '${_system_name}-${_system_version}' here: https://github.com/rvm/rvm/issues"
  return 49
}

requirements_centos_before_update_check_rhn_channel()
{
  __rvm_try_sudo rhn-channel -l | grep optional >/dev/null ||
  {
    \typeset __package_name="$(
      __rvm_try_sudo rhn-channel -l |
      awk '/^rhel-'"${_system_arch}"'-(server|client|workstation)-[0-9]+$/{print gensub(/(-[0-9]+)$/, "-optional\\1", "g")}'
    )"
    rvm_log "Enabling optional repository"
    rvm_requiremnts_fail_or_run_action 2 \
      "It is not possible to enable optional repository for $*, enable it using: rhn-channel -a -c ${__package_name}" \
      __rvm_try_sudo rhn-channel -a -c "${__package_name}" ||
      return $?
  }
}

requirements_centos_before_install_epel()
{
  "${rvm_scripts_path}/fetch" "${epel_key}"
  "${rvm_scripts_path}/fetch" "${epel_rpm}"
  __rvm_try_sudo rpm --import   "${rvm_archives_path}/${epel_key##*/}"
  __rvm_try_sudo rpm --quiet -i "${rvm_archives_path}/${epel_rpm##*/}" ||
  {
    \typeset __ret=$?
    rvm_error "Error installing EPEL, it is required for libyaml-devel,
either there was an error installing EPEL package,
or there was problem checking if libyaml-devel is available / installed."
    return ${__ret}
  }
}

requirements_centos_before_update_check_epel()
{
  [[ " $* " == *" libyaml-devel "* ]] || return 0
  # if you change this, change the scripts/functions/pkg version too
  [[ -f /etc/yum.repos.d/epel.repo ]] ||
  {
    \typeset version="${_system_version%%.*}"
    __rvm_db "epel${version}_key" "epel_key"
    __rvm_db "epel${version}_${_system_arch}_rpm" "epel_rpm"
    if
      [[ -z "$epel_rpm" || -z "$epel_key" ]]
    then
      requirements_centos_before_update_check_repos_missing_error $* || return $?
    fi
    rvm_requiremnts_fail_or_run_action 2 \
      "It is not possible to install EPEL repository for YAML, install it using: $epel_rpm" \
      __rvm_log_command centos_install_epel "Installing EPEL repository" requirements_centos_before_install_epel ||
      return $?
  }
}

# return 0  when found missing packages
# return >0 when no missing packages
requirements_centos_before_update_check_missing_packages()
{
  \typeset -a __packages_to_check
  \typeset __package_name
  case "${_system_name_lowercase}" in
    (redhat)
      __packages_to_check=( libyaml-devel libffi-devel )
      ;;
    (centos)
      __packages_to_check=( libyaml-devel )
      ;;
    (*)
      return 1
      ;;
  esac
  __missing_packages=()
  for __package_name in "${__packages_to_check[@]}"
  do
    if
      [[ " ${packages_to_install[*]} " == *" ${__package_name} "* ]] &&
      ! requirements_centos_lib_installed "${__package_name}" &&
      ! requirements_centos_lib_available_no_caching "${__package_name}"
    then
      __missing_packages+=( "${__package_name}" )
    fi
  done
  [[ ${#__missing_packages[*]} -gt 0 ]] || return 2
}

requirements_centos_before_update_check_repos()
{
  \typeset -a __missing_packages
  requirements_centos_before_update_check_missing_packages || return 0

  case "${_system_name_lowercase}" in
    (redhat)
      requirements_centos_before_update_check_rhn_channel "${__missing_packages[@]}" || return $?
      ;;
    (centos)
      requirements_centos_before_update_check_epel "${__missing_packages[@]}" || return $?
      ;;
    (*)
      requirements_centos_before_update_check_repos_missing_error "${__missing_packages[@]}" || return $?
      ;;
  esac
}

requirements_centos_before_update()
{
  case "$1" in
    (ruby-1*)
      requirements_centos_before_update_check_repos || return $?
      ;;
  esac
}

requirements_centos_check_binary()
{
  {
    __rvm_which update-alternatives >/dev/null &&
    update-alternatives --display "$1" >/dev/null 2>&1
  } ||
  __rvm_which "$1" >/dev/null || return $?
}

requirements_centos_define_glibc()
{
  requirements_check glibc-headers glibc-devel
}

requirements_centos_define_libyaml()
{
  case "$1" in
    (ruby-1*) requirements_check libyaml-devel ;;
  esac
}

requirements_centos_define_openssl()
{
  requirements_check openssl-devel
}

requirements_centos_define()
{
  case "$1" in
    (rvm)
      requirements_check bash curl patch
      ;;

    (jruby*)
      requirements_check make

      if
        is_head_or_disable_binary "$1"
      then
        requirements_centos_check_binary javac ||
          requirements_check_fallback java-1.8.0-openjdk-devel java-devel ||
          requirements_check_fallback java-1.7.0-openjdk-devel java-devel
        requirements_check git
        case $( jruby_installation_method  "$1" ) in
          mvn) requirements_check_custom_after mvn=maven ;;
        esac
      else
        requirements_centos_check_binary java ||
          requirements_check_fallback java-1.8.0-openjdk java ||
          requirements_check_fallback java-1.7.0-openjdk java
      fi
      ;;

    (rbx*|rubinius*)
      requirements_check automake bison clang-3.6 flex gdbm-devel git libyaml-devel llvm-devel llvm-static \
                         make ncurses-devel openssl-devel readline-devel ruby-devel rubygems zlib-devel
      ;;

    (truffleruby*)
      requirements_check zlib-devel make gcc libxml2 libyaml-devel
      requirements_${_system_name_lowercase}_define_openssl $1
      ;;

    (ir*)
      true # not that easy
      ;;

    (opal)
      true # not that easy
      ;;

    (*-head)
      requirements_check git
      requirements_centos_define "${1%-head}"
      requirements_version_minimal autoconf 2.67
      ;;

    (*)
      if [[ ${#rvm_patch_names[@]} -gt 0 ]]
      then requirements_version_minimal autoconf 2.67
      fi

      requirements_check autoconf automake bison bzip2 gcc-c++ libffi-devel libtool make patch readline \
                         readline-devel ruby sqlite-devel zlib zlib-devel

      requirements_${_system_name_lowercase}_define_glibc $1
      requirements_${_system_name_lowercase}_define_libyaml $1
      requirements_${_system_name_lowercase}_define_openssl $1
      ;;
  esac
}
